using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;

/// <summary>
/// Summary description for MonHocDAO
/// </summary>
public class MonHocDAO:DataProvider
{
	public MonHocDAO()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public ArrayList LayMang_MonHoc()
    {
        connect();
        ArrayList arr = new ArrayList();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_Select_MonHoc", strCnn);
            cmd.CommandType = CommandType.StoredProcedure;
            
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                MonHocDTO mondto = new MonHocDTO();
                mondto.MaMonHoc = dr.GetString(0);
                mondto.TenMon = dr.GetString(1);
                mondto.SoTinChi = dr.GetInt32(2);
                mondto.DeCuong = dr.GetString(3);
                mondto.MaLoai = dr.GetInt32(4);
                mondto.MaBoMon = dr.GetInt32(5);
                arr.Add(mondto);
            }
        }
        catch (Exception es)
        {
            throw es;
        }
        return arr;
    }
    public DataTable getBangMonHocChiTiet(int bomon)
    {
        connect();
        DataTable dt;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_Select_MonHocChiTiet", strCnn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maBoMon", SqlDbType.Int);
            cmd.Parameters["@maBoMon"].Value = bomon;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dt = ds.Tables[0];         
        }
        catch (Exception es)
        {
            throw es;
        }
        finally
        {
            disconnect();
        }
        return dt;
    }
    public DataTable getBangMonHocTienQuyetChiTiet(string mamonhoc)
    {
        connect();
        DataTable dt = new DataTable();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_Select_MonHocTienQuyet", strCnn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maMonHoc", SqlDbType.NVarChar);
            cmd.Parameters["@maMonHoc"].Value = mamonhoc;
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();
            da.Fill(ds);
            dt = ds.Tables[0];   

        }
        catch (Exception es)
        {
            throw es;
        }
        finally
        {
            disconnect();
        }
        return dt;
    }
    public MonHocDTO getMonHoc(String ma)
    {
        connect();
        MonHocDTO mondto = new MonHocDTO();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_Select_MonHocTheoMa", strCnn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@MaMonHoc", SqlDbType.NVarChar);
            cmd.Parameters["@MaMonHoc"].Value = ma;

            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {

                mondto.MaMonHoc = dr.GetString(0);
                mondto.TenMon = dr.GetString(1);
                mondto.SoTinChi = dr.GetInt32(2);
                mondto.DeCuong = dr.GetString(3);
                mondto.MaLoai = dr.GetInt32(4);
                mondto.MaBoMon = dr.GetInt32(5);
                mondto.TenLoai = dr.GetString(6);
                mondto.TenBoMon = dr.GetString(7);
            }
        }
        catch (Exception es)
        {
            throw es;
        }
        finally
        {
            disconnect();
        }
        return mondto;
    }
    public DataTable LayDanhSachMonHoc()
    {
        connect();
        DataTable dt = new DataTable();
        MonHocDTO mondto = new MonHocDTO();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_Select_MonHocTomTat", strCnn);
            cmd.CommandType = CommandType.StoredProcedure;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dt = ds.Tables[0];
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        finally
        {
            disconnect();
        }
        return dt;
    }
    public Boolean insert(MonHocDTO mondto)
    {
        connect();
        Boolean flag = true;
        try
        {
            SqlCommand cmd = new SqlCommand("sp_Insert_MonHoc", strCnn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@tenmon", SqlDbType.NVarChar);
            cmd.Parameters["@tenmon"].Value = mondto.TenMon.ToString();

            cmd.Parameters.Add("@sotinchi", SqlDbType.Int);
            cmd.Parameters["@sotinchi"].Value = mondto.SoTinChi;

            cmd.Parameters.Add("@decuong", SqlDbType.NVarChar);
            cmd.Parameters["@decuong"].Value = mondto.DeCuong.ToString();

            cmd.Parameters.Add("@maloai", SqlDbType.Int);
            cmd.Parameters["@maLoai"].Value = mondto.MaLoai;

            cmd.Parameters.Add("@mabomon", SqlDbType.Int);
            cmd.Parameters["@mabomon"].Value = mondto.MaBoMon;

            cmd.Parameters.Add("@mamonhoc", SqlDbType.NVarChar);
            cmd.Parameters["@mamonhoc"].Value = mondto.MaMonHoc;

            cmd.ExecuteNonQuery();
            


        }
        catch (Exception es)
        {
            throw es;
            flag = false;
        }
        finally
        {
            disconnect();
        }
        return flag;
    }

    public void delete(string mamon)
    {
        connect();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_Delete_MonHoc", strCnn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@maMonHoc", SqlDbType.NVarChar);
            cmd.Parameters["@maMonHoc"].Value = mamon;
            cmd.ExecuteNonQuery();

        }
        catch (Exception es)
        {
            throw es;
        }
    }

    public void update(MonHocDTO mondto)
    {
        connect();
        try
        {
            SqlCommand cmd = new SqlCommand("sp_Update_MonHoc", strCnn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@tenMon", SqlDbType.NVarChar);
            cmd.Parameters["@tenMon"].Value = mondto.TenMon.ToString();

            cmd.Parameters.Add("@soTinChi", SqlDbType.Int);
            cmd.Parameters["@soTinChi"].Value = mondto.SoTinChi;

            cmd.Parameters.Add("@deCuong", SqlDbType.NVarChar);
            cmd.Parameters["@deCuong"].Value = mondto.DeCuong.ToString();

            cmd.Parameters.Add("@maLoai", SqlDbType.Int);
            cmd.Parameters["@maLoai"].Value = mondto.MaLoai;

            cmd.Parameters.Add("@maBoMon", SqlDbType.Int);
            cmd.Parameters["@maBoMon"].Value = mondto.MaBoMon;

            cmd.Parameters.Add("@maMonHoc", SqlDbType.NVarChar);
            cmd.Parameters["@maMonHoc"].Value = mondto.MaMonHoc;

            cmd.ExecuteNonQuery();
        }
        catch (System.Exception ex)
        {
            throw ex;
        }
        finally
        {
            disconnect();
        }

    }
}
